package com.bdwm.controller;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.bdwm.dao.UserDBService;
import com.bdwm.model.Constants;
import com.bdwm.model.User;
import com.bdwm.util.ParseRequestPara;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/")
public class UserManageController {
	@RequestMapping(method = RequestMethod.GET)
	public String rootUrl(ModelMap model) {
		model.addAttribute("massage", "request url error");
		return "welcome";
	}
	
	//http://localhost:8080/DeviceManage/getUser?userName=wangwenhui
	@RequestMapping(value = "/getUser", method = RequestMethod.GET)
	public void getUserInfo(HttpServletResponse response, HttpServletRequest request) throws IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		String userName = ParseRequestPara.parseRequestParameter(request.getParameter("userName"));
		if(ParseRequestPara.isNull(userName)){
			response.getWriter().println("{\"status\":1 ,\"error_info\":\"userName is null!\"}");
			response.getWriter().close();
			return;
		}
		
		boolean isExist = UserDBService.findIfUserExist(userName);
		if(!isExist) {
			response.getWriter().println("{\"status\":1 ,\"error_info\":\"user not exist!\"}");
			response.getWriter().close();
			return;
		}
		User user = UserDBService.getUserInfo(userName);
		//System.out.println(user.toString());
		//output.println(user.toString());
		response.getWriter().println("{\"status\":0 ,\"data\":{"+user.toString()+"}}");
		response.getWriter().close();
	}
	
	//http://localhost:8080/DeviceManage/addUser?userName=test&userEmail=test@baiduwaimai.com&userDept=test&userTel=15010011001
	@RequestMapping(value = "/addUser", method = RequestMethod.GET)
	public void addUserInfo(HttpServletResponse response, HttpServletRequest request) throws IOException, ClassNotFoundException, SQLException {
		ServletOutputStream output = response.getOutputStream();
		request.setCharacterEncoding("utf-8");
		User user = new User();
		user.setUserName(ParseRequestPara.parseRequestParameter(request.getParameter("userName")));
		user.setUserEmail(ParseRequestPara.parseRequestParameter(request.getParameter("userEmail")));
		user.setUserDept(ParseRequestPara.parseRequestParameter(request.getParameter("userDept")));
		user.setUserTel(ParseRequestPara.parseRequestParameter(request.getParameter("userTel")));
		user.setPassword(ParseRequestPara.parseRequestParameter(request.getParameter("password")));
		String checkPara = ParseRequestPara.checkUserParameter(user);
		JSONObject json = new JSONObject();
		if (!"success".equals(checkPara)) {
			json.accumulate("status", 1);
			json.accumulate("message", checkPara);
			output.println(json.toString());
			return;
		}
		//System.out.println(user);
		boolean isExist = UserDBService.findIfUserExist(user.getUserName());
		if(isExist) {
			json.accumulate("status", 1);
			json.accumulate("message", "user already exist!");
			output.println(json.toString());
			return;
		}
		int status = UserDBService.addUser(user);
		if(Constants.ERROR == status) {
			json.accumulate("status", 1);
			json.accumulate("message", "save user error!");
			output.println(json.toString());
//			output.println("{\"status\":1 ,\"error_info\":\"save user error!\"}");
			return;
		}
		json.accumulate("status", 0);
		json.accumulate("message", "save user success!");
		output.println(json.toString());
//		output.println("{\"status\":0 ,\"error_info\":\"save user successed!\"}");
	}
	
	
}